﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsuranceSell.Code.Entities;
using InsuranceSell.Code.Services;
using System.Globalization;

namespace InsuranceSell
{
    public partial class frmAddNew : Form
    {
        public frmAddNew()
        {
            InitializeComponent();
        }

        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {
            //mac dinh ngay ket thuc bao hiem luon cach ngay bat dau 1 nam
            DateTime Temp;
            Temp = dtpStartDate.Value;
            dtpEndDate.Value = new DateTime(Temp.Year + 1, Temp.Month, Temp.Day);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtCustomerName.Text.Trim() != "" && txtContractNo.Text.Trim() != "" && dtpEndDate.Value != dtpStartDate.Value && cbbIsuranceType.SelectedIndex != -1 && txtPresentativeName.Text != "")
            {
                Customer customer = new Customer();

                customer.CustomerName = txtCustomerName.Text.Trim();
                customer.ContractNo = txtContractNo.Text.Trim();
                customer.StartDate = dtpStartDate.Value;
                customer.EndDate = dtpEndDate.Value;
                customer.Address = txtAddress.Text.Trim();
                customer.Telephone = txtTelephone.Text.Trim();
                customer.Mobile = txtMobile.Text.Trim();
                customer.Email = txtMobile.Text.Trim();
                customer.TaxCode = txtTaxCode.Text.Trim();
                customer.BankAccount = txtAccountBank.Text.Trim();
                customer.BankName = txtBankName.Text.Trim();
                customer.PresentativeName = txtPresentativeName.Text.Trim();
                customer.PresentativeJobTitle = txtPresentativeJobTitle.Text.Trim();
                customer.InsuranceType = cbbIsuranceType.SelectedItem.ToString();
                customer.InsuranceTarget = txtInsuranceTarget.Text.Trim();

                bool isAdded = CustomerServices.AddNewCustomer(customer);

                if (isAdded == true)
                {
                    MessageBox.Show("Đã thêm thành công!", "Thêm khách hàng");
                }
                else
                {
                    MessageBox.Show("Hợp đồng này đã tồn tại!", "Thêm khách hàng");
                }
            }
            else {
                MessageBox.Show("Chưa nhập đủ thông tin cần thiết!", "Lỗi Nhập Dữ Liệu");
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            txtAccountBank.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtBankName.Text = string.Empty;
            txtContractNo.Text = string.Empty;
            txtCustomerName.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtInsuranceTarget.Text = string.Empty;
            txtMobile.Text = string.Empty;
            txtPresentativeJobTitle.Text = string.Empty;
            txtPresentativeName.Text = string.Empty;
            txtTaxCode.Text = string.Empty;
            txtTelephone.Text = string.Empty;
            dtpStartDate.Value = DateTime.Now;
            dtpEndDate.Value = DateTime.Now;
            cbbIsuranceType.SelectedIndex = -1;
            txtCustomerName.Focus();
        }

        private void cbbIsuranceType_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult sureDelete=MessageBox.Show("Bạn có chắc muốn xóa Bảo hiểm này không ?", "Xác nhận xóa bảo hiểm", MessageBoxButtons.OKCancel);
            if (sureDelete==DialogResult.OK)
            {
                if (CustomerServices.DeleteCustomer(txtContractNo.Text))
                {
                    MessageBox.Show("Xóa bảo hiểm thành công !", "Xóa Bảo Hiểm");
                }
                else
                {
                    MessageBox.Show("Không thể xóa bảo hiểm này !", "Lỗi xóa bảo hiểm");
                }
                DataTable load50NewCustomers = CustomerServices.LoadAllCustomer();

                if (load50NewCustomers != null)
                {
                    dtg50NewCustomer.DataSource = load50NewCustomers;
                    load50NewCustomers.Dispose();
                }
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            if (txtCustomerName.Text.Trim() != "" && txtContractNo.Text.Trim() != "" && dtpEndDate.Value != dtpStartDate.Value && cbbIsuranceType.SelectedIndex != -1 && txtPresentativeName.Text != "")
            {
                Customer customer = new Customer();

                customer.CustomerName = txtCustomerName.Text.Trim();
                customer.ContractNo = txtContractNo.Text.Trim();
                customer.StartDate = dtpStartDate.Value;
                customer.EndDate = dtpEndDate.Value;
                customer.Address = txtAddress.Text.Trim();
                customer.Telephone = txtTelephone.Text.Trim();
                customer.Mobile = txtMobile.Text.Trim();
                customer.Email = txtMobile.Text.Trim();
                customer.TaxCode = txtTaxCode.Text.Trim();
                customer.BankAccount = txtAccountBank.Text.Trim();
                customer.BankName = txtBankName.Text.Trim();
                customer.PresentativeName = txtPresentativeName.Text.Trim();
                customer.PresentativeJobTitle = txtPresentativeJobTitle.Text.Trim();
                customer.InsuranceType = cbbIsuranceType.SelectedItem.ToString();
                customer.InsuranceTarget = txtInsuranceTarget.Text.Trim();

                bool isUpdated = CustomerServices.UpdateCustomer(customer);

                if (isUpdated == true)
                {
                    MessageBox.Show("Đã Sửa thành công!", "Sửa thông tin khách hàng");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Có lỗi !", "Sửa thông tin khách hàng");
                }
            }
            else
            {
                MessageBox.Show("Chưa nhập đủ thông tin cần thiết!", "Lỗi Nhập Dữ Liệu");
            }
            
        }

        private void frmAddNew_Load(object sender, EventArgs e)
        {

            //CultureInfo culture = CultureInfo.CreateSpecificCulture("vi");
            //Application.CurrentCulture = culture;

            dtpEndDate.Format = DateTimePickerFormat.Custom;
            //string[] format = dtpEndDate.Value.GetDateTimeFormats(Application.CurrentCulture);
            dtpEndDate.CustomFormat = "dd-MM-yyyy";
            dtpStartDate.Format = DateTimePickerFormat.Custom;
            dtpStartDate.CustomFormat = "dd-MM-yyyy";

            
            DataTable load50NewCustomers = CustomerServices.LoadAllCustomer();

            if (load50NewCustomers != null)
            {
                dtg50NewCustomer.DataSource = load50NewCustomers;
                load50NewCustomers.Dispose();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            DataTable findContractNo = CustomerServices.FindContractNo(txtContractNo.Text.Trim());
            if (findContractNo != null)
            {
                dtg50NewCustomer.DataSource = findContractNo;
            }
            else {
                MessageBox.Show("Không tìm thấy Hợp đồng tương ứng !");
            }
        }

        
        private void dtg50NewCustomer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtAccountBank.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[12].Value.ToString();
            txtAddress.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[6].Value.ToString();
            txtBankName.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[11].Value.ToString();
            txtContractNo.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtCustomerName.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[0].Value.ToString();
            txtEmail.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[9].Value.ToString();
            txtInsuranceTarget.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[3].Value.ToString();
            txtMobile.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[7].Value.ToString();
            txtTelephone.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[8].Value.ToString();
            txtPresentativeJobTitle.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[14].Value.ToString();
            txtPresentativeName.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[13].Value.ToString();
            txtTaxCode.Text = dtg50NewCustomer.Rows[e.RowIndex].Cells[10].Value.ToString();
            dtpStartDate.Value = DateTime.Parse(dtg50NewCustomer.Rows[e.RowIndex].Cells[4].Value.ToString());
            dtpEndDate.Value = DateTime.Parse(dtg50NewCustomer.Rows[e.RowIndex].Cells[5].Value.ToString());
            cbbIsuranceType.SelectedItem = dtg50NewCustomer.Rows[e.RowIndex].Cells[2].Value.ToString();
        }


    }
}